﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Web.UI;
using GTS.Channel.Api.Entity;
using Pub.Class;
using GTS.Channel.Api.Helper;

namespace GTS.Channel.Api {
    /// <summary>
    /// 米飞SMS和IVR的MO MT MR接收数据接口
    /// </summary>
    public class MiFei : ISMSIVR {
        /// <summary>
        /// SPCode
        /// </summary>
        public string SPCode = "MiFei";
        /// <summary>
        /// SMS MO的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMOData() {
            //初始值
            ChannelDealData info = new ChannelDealData();
            info.SPCode = SPCode;
            info.SMSKey = 1;
            info.IsHidden = false;

            //接收值
            string result = Request2.GetRequestInputStream().ReplaceRN(" ");
            info.LinkID = result.GetJsonString("moid").ToString();
            info.Mobile = result.GetJsonString("msisdn").Replace("+", "").Trim();
            info.ReceiveQuery = result.GetJsonString("content").Trim();
            info.MODate = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(result.GetJsonString("timestamp").ToBigInt()).ToString("yyyy-MM-dd HH:mm:ss").ToDateTime();
            info.LongPhone = result.GetJsonString("shortcode").ToString();
            info.Telcoid = result.GetJsonString("teleco").Trim();
            info.CountryCode = result.GetJsonString("mcc").ToString().Trim();
            info.PLMN = result.GetJsonString("plmn").Trim();
            info.Price = result.GetJsonString("price").ToDecimal();
            info.Unit = result.GetJsonString("currency").Trim();

            if (info.Price <= 0 && info.CountryCode != "510") Base.WriteLog("mo金额错误：" + info.Price + ",json:" + result);
            if (info.Mobile.IsNullEmpty() || info.LinkID.IsNullEmpty() || info.ReceiveQuery.IsNullEmpty() || info.LongPhone.IsNullEmpty()) return null;
            if (info.CountryCode == "602") info.ResponseStatus = 1;

            info = Base.GetInfo(info);

            Pub.Class.Log.Write(Server2.GetMapPath("/logdetail/" + DateTime.Now.ToDate() + "_" + SPCode + ".txt"), "MO（" + DateTime.Now + "）：" + result);

            return info;
        }
        /// <summary>
        /// SMS MT的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMTData() {
            //初始值
            ChannelDealData info = new ChannelDealData();
            info.SPCode = SPCode;
            info.SMSKey = 3;

            //接收值
            string result = Request2.GetRequestInputStream().ReplaceRN(" ");
            info.LinkID = result.GetJsonString("moid").ToString();
            info.Mobile = result.GetJsonString("msisdn").Trim();
            info.SMSContent = result.GetJsonString("content").Trim();
            info.MRDate = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(result.GetJsonString("timestamp").ToBigInt()).ToString("yyyy-MM-dd HH:mm:ss").ToDateTime();
            info.LongPhone = result.GetJsonString("shortcode").ToString();
            info.Telcoid = result.GetJsonString("teleco").Trim();
            info.CountryCode = result.GetJsonString("mcc").ToString().Trim();
            info.Unit = result.GetJsonString("currency").Trim();
            info.SMSKey = result.GetJsonString("istest").ToInt();
            info.ResponseStatus = 1;
            int price = result.GetJsonString("price").ToInt();
            if (info.CountryCode == "510" && price > 0) info.Price = price;
            if (info.LinkID.IsNullEmpty()) return null;

            Pub.Class.Log.Write(Server2.GetMapPath("/logdetail/" + DateTime.Now.ToDate() + "_" + SPCode + ".txt"), "MT（" + DateTime.Now + "）：" + result);

            return info;
        }
        /// <summary>
        /// SMS MR的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMRData() {
            //初始值
            ChannelDealData info = new ChannelDealData();
            info.SPCode = SPCode;
            info.SMSKey = 2;

            //接收值
            string result = Request2.GetRequestInputStream().ReplaceRN(" ");
            info.LinkID = result.GetJsonString("moid").ToString();
            info.Mobile = result.GetJsonString("msisdn").Trim();
            info.MRDate = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(result.GetJsonString("timestamp").ToBigInt()).ToString("yyyy-MM-dd HH:mm:ss").ToDateTime();
            info.SMSKey = result.GetJsonString("istest").ToInt();
            info.ResponseStatus = result.GetJsonString("status") == "OK" ? 1 : 0;
            if (info.LinkID.IsNullEmpty()) return null;

            Pub.Class.Log.Write(Server2.GetMapPath("/logdetail/" + DateTime.Now.ToDate() + "_" + SPCode + ".txt"), "MR（" + DateTime.Now + "）：" + result);

            return info;
        }
        /// <summary>
        /// IVR的参数数据
        /// </summary>
        /// <returns></returns>
        public IVRDealData GetIVRMRData() {
            return null;
        }
        /// <summary>
        /// 输出消息
        /// </summary>
        /// <param name="res"></param>
        public void Output(ResultEnum res) {
            Msg.WriteEnd(res == ResultEnum.Success ? "{\"status\":\"OK\"}" : "error");
        }
    }
}

